using Autofac;
using Microsoft.AspNetCore.Builder;
using MongoDB.Driver;
using SqlSugar;
using XjjXmm.Infrastructure.Configuration;
using XjjXmm.Infrastructure.Constant;
using XjjXmm.Infrastructure.Mongo;
using XjjXmm.Infrastructure.ToolKit;

namespace xjjxmm.infrastructure.SetUp
{
	public static class MongoExtension
	{
		public static ContainerBuilder AddMongo(this ContainerBuilder builder)
		{
			
				if (!ConfigHelper.GetConfig(CommonConstant.MongoConfigKey).IsNullOrEmpty())
				{
					builder.RegisterGeneric(typeof(MongoRepository<>)).InstancePerLifetimeScope();
					
					builder.Register(com =>
					{
						var connectionString = ConfigHelper.GetConfig("mongodb:connectiongString");
						var database = ConfigHelper.GetConfig("mongodb:database");
						var client = new MongoClient(connectionString);
						return client.GetDatabase(database);
				
					}).AsImplementedInterfaces().SingleInstance();
				}
				
				return builder;
		}

       
    }
}
